/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package my.ClientTorrent;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;

/**
 *
 * @author Wico
 */
public class ClientTorrent {

    private String identifier;
    private ArrayList<Torrent> listTorrent;
    private static int port;
    public static int trakerPort;
    private ServerSocket serverSocket;
    private Thread thread;
    
    public ClientTorrent() throws IOException
    {
        identifier = "TonoTor";
        listTorrent = new ArrayList<Torrent>();
        port = 1234;
        trakerPort = 3096;
        serverSocket = new ServerSocket(port);
        thread = new Thread(new Runnable() {

            @Override
            public void run() {
                while(true)
                {
                    try {
                        Socket socket = serverSocket.accept();
                        InputStream inputStream = socket.getInputStream();
                        byte[] len = new byte[1];
                        byte[] pstr = null;
                        byte[] reserved = new byte[8];
                        byte[] infohash = new byte[20];
                        inputStream.read(len);
                        pstr = new byte[len[0]];
                        inputStream.read(pstr);
                        inputStream.read(reserved);
                        inputStream.read(infohash);
                       
                        int indeks = findTorrent(new String(infohash));
                        if (indeks != -1)
                        {
                            String ipAddress = socket.getInetAddress().getHostAddress();
                            int port = socket.getPort();
                        }
                        //inputStream.r
                    } catch (IOException ex) {
                        System.out.println("Socket gagal membentuk koneksi dengan peer lain.");
                    }
                }
            }
        });
    }
    
    public ClientTorrent(int port) throws IOException
    {
        identifier = "TonoTor";
        listTorrent = new ArrayList<Torrent>();
        this.port = port;
        trakerPort = 3096;
        serverSocket = new ServerSocket(port);
    }
    
    public void closeSocket()
    {
        thread.interrupt();
    }
    
    public int findTorrent(String _infoHash)
    {
        int indeks = -1;
        for (int i = 0; i < listTorrent.size(); i++)
        {
            if (_infoHash.compareTo(listTorrent.get(i).getInfoHash()) == 0)
            {
                indeks = i;
                break;
            }
        }
        return indeks;
    }
    
    public void addTorrent(FileTorrent fileTorrent, String location)
    {
        listTorrent.add(new Torrent(fileTorrent, location)); 
    }
    
    public void removeTorrent(int idx,boolean deleteAll)
    {
        if (deleteAll)
        {
            listTorrent.get(idx).getDownloadedFile().delete();
        }
        listTorrent.remove(idx);
    }
    
    public void startTorrent(int idx)
    {
        Thread haha = new Thread(listTorrent.get(idx));
        haha.start();
    }
    
    public void stopTorrent(int idx)
    {
        listTorrent.get(idx).setState((byte)0);
    }
    /**
     * @return the port
     */
    public static int getPort() {
        return port;
    }

    /**
     * @param aPort the port to set
     */
    public static void setPort(int aPort) {
        port = aPort;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws UnsupportedEncodingException, NoSuchAlgorithmException, FileNotFoundException, IOException {
       ClientTorrent ct = new ClientTorrent(30561);
       
//       FileTorrent fileTorrent = FileTorrent.ConvertToTorrent(new File("D:\\Master Softwares\\Windows\\IDM.zip"), "http://localhost/announce", 0);
       FileTorrent fileTorrent = FileTorrent.ConvertToTorrent(new File("D:/Back_UP/b.exe"), "http://localhost/announce", 0);
       fileTorrent.SaveTo("D:/hha.torrent");
       ct.addTorrent(fileTorrent,"D:/Back_UP/");
      // ct.startTorrent(0);
       ct.getListTorrent().get(0).cekDownloaded();
       System.out.println(ct.getListTorrent().get(0).getLeft());
       
        //System.out.println(Math.ceil(2.1f));
    }

    /**
     * @return the listTorrent
     */
    public ArrayList<Torrent> getListTorrent() {
        return listTorrent;
    }

    /**
     * @param listTorrent the listTorrent to set
     */
    public void setListTorrent(ArrayList<Torrent> listTorrent) {
        this.listTorrent = listTorrent;
    }
}
